package zelix.hack.hacks;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import net.minecraft.client.Minecraft;
import zelix.hack.Hack;
import zelix.hack.HackCategory;
import zelix.utils.hooks.visual.ChatUtils;
import zelix.value.BooleanValue;
import zelix.value.Mode;
import zelix.value.ModeValue;
import zelix.value.NumberValue;
import zelix.utils.Wrapper;

import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.network.play.client.CPacketPlayer;
import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent;
import zelix.utils.system.Connection;

public class Flight extends Hack {

	//hyt
	Queue<CPacketPlayer> packets = new LinkedList<>();
	List<CPacketPlayer> p=new ArrayList<CPacketPlayer>();
	private boolean isfly;
	private int stage =0;
	private int maxstage=5;
	private boolean jump=false;

	Minecraft mc = Wrapper.INSTANCE.mc();
	private int timer=0;
	private double y=0;
	
	private NumberValue motionY;
	private NumberValue motionXZ;
	private NumberValue delay;
	private NumberValue flytimer;

	private BooleanValue XYZ;
	boolean send = false;
	//
	public ModeValue mode;
	int ticks = 0;
	public Flight() {
		super("Flight", HackCategory.MOVEMENT);
		
		this.mode = new ModeValue("Mode", new Mode("Simple", true), new Mode("Dynamic", false), new Mode("Hypixel", false),new Mode("HYT", false));
		this.motionY=new NumberValue("MotionY", 0.5D, 0.42D, 6D);
		this.motionXZ=new NumberValue("MotionXZ", 1D, 0D, 10D);
		this.delay=new NumberValue("Delay", 2D, 0D, 10D);
		this.flytimer=new NumberValue("FlyTimer", 1D, 0.1D, 10D);
		this.XYZ = new BooleanValue("AntiKick" , true);
		
		this.addValue(mode,motionY,motionXZ,delay,flytimer,XYZ);
	}
	
	@Override
	public String getDescription() {
		return "Allows you to you fly.";
	}
	
	@Override
	public void onEnable() {
		ticks = 0;
		y= Wrapper.INSTANCE.player().posY;
		timer=999;
		super.onEnable();
	}
	
	@Override
	public void onClientTick(ClientTickEvent event) {
		EntityPlayerSP player = Wrapper.INSTANCE.player();
		if(mode.getMode("Hypixel").isToggled()) {
			player.motionY = 0.0;
			player.setSprinting(true);
			player.onGround = true;
		    ticks++;
		    if(ticks == 2 || ticks == 4 || ticks == 6 || ticks == 8 || ticks == 10 || ticks == 12 || ticks == 14 || ticks == 16 || ticks == 18 || ticks == 20) {
		    player.setPosition(player.posX, player.posY + 0.00000000128, player.posZ);
		    } if(ticks == 20) {
		    	ticks = 0;
		    }
		}
		else if(mode.getMode("Simple").isToggled())
		{
			player.capabilities.isFlying = true;
		}
		else if(mode.getMode("Dynamic").isToggled())
		{
			if (XYZ.getValue() && mc.player.ticksExisted % 4 == 0) {mc.player.motionY = -0.04f;}
			float flyspeed = flytimer.getValue().floatValue();
			player.jumpMovementFactor = 0.4f;
			player.motionX = 0.0;
			player.motionY = 0.0;
			player.motionZ = 0.0;
			player.jumpMovementFactor *= (float) flyspeed * 3f;
	        if (Wrapper.INSTANCE.mcSettings().keyBindJump.isKeyDown()) {
				if (XYZ.getValue()) {
					mc.player.motionY = mc.player.ticksExisted % 20 == 0 ? -0.04f : flyspeed;
				}else {
					player.motionY += flyspeed;
				}
			}
		}else if(mode.getMode("HYT").isToggled()) {
			if(isfly == false) {
				timer++;
			}
			if(timer>delay.getValue()){
	            timer = 0;
				move();
	            isfly = true;
	            stage = 0;
	        }
			if (stage >= 1) {
	            isfly = false;
	            if(packets!=null&&!packets.isEmpty()) {
	            Wrapper.INSTANCE.sendPacket(packets.poll());
	           // ChatUtils.message("ok");
	            }
			}
			
		}
		super.onClientTick(event);
	}
	@Override
	public boolean onPacket(Object packet, Connection.Side side) {

		if (packet instanceof CPacketPlayer&&mode.getMode("HYT").isToggled()) {
			ChatUtils.message("Packets:" + String.valueOf(packets.size()));
			packets.add((CPacketPlayer) packet);
			stage++;
			return !isfly;
		}
		return true;
	}
	@Override
	public void onDisable() {
		if(mode.getMode("Simple").isToggled()) {
			Wrapper.INSTANCE.player().capabilities.isFlying = false;
		}
		
		if(mode.getMode("HYT").isToggled()) {
			if(packets!=null&&!packets.isEmpty()) {
		Wrapper.INSTANCE.sendPacket(packets.poll());
			}
		}
		
		packets.clear();
		super.onDisable();
	}
	
	private void move() {
		
		 if (Wrapper.INSTANCE.player().posY<=y) {
		        Wrapper.INSTANCE.player().motionY = motionY.getValue();
		    } else {
		        double dir = Wrapper.INSTANCE.player().rotationYaw / 180 * Math.PI;
		        if (Wrapper.INSTANCE.player().motionY < 0) 
		        	Wrapper.INSTANCE.player().motionY = -0.05;
		        Wrapper.INSTANCE.player().motionX = -Math.sin(dir) * motionXZ.getValue();
		        Wrapper.INSTANCE.player().motionZ = Math.cos(dir) * motionXZ.getValue();
		    }
		
	}

	
}
